﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmNgheNghiep : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmNgheNghiepEvent;
        int State = 0;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmNgheNghiepBusiness dmNgheNghiepBusiness = new DmNgheNghiepBusiness();
        DmNgheNghiepEntity dmNgheNghiepEntity = null;

        public frmCapNhatDmNgheNghiep(int state)
        {
            InitializeComponent();
            State = state;
        }

        public frmCapNhatDmNgheNghiep(int state, DmNgheNghiepEntity obj)
        {
            InitializeComponent();

            State = state;
            dmNgheNghiepEntity = obj;

            txtMaNgheNghiep.Text = dmNgheNghiepEntity.MaNgheNghiep;
            txtTenNgheNghiep.Text = dmNgheNghiepEntity.TenNgheNghiep;
            txtMoTa.Text = dmNgheNghiepEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmNgheNghiepEntity.TrangThaiSuDung;
            txtTenNgheNghiep.Focus();
        }

        private void frmcapnhatDMnghenghiep_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaNgheNghiep, "Mã nghề nghiệp") == false) return;

            dmNgheNghiepEntity = new DmNgheNghiepEntity()
            {
                MaNgheNghiep = txtMaNgheNghiep.Text,
                TenNgheNghiep = txtTenNgheNghiep.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmNgheNghiepBusiness.GetDmNgheNghiep(txtMaNgheNghiep.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaNgheNghiep, "Mã nghề nghiệp") == false) return;

                dmNgheNghiepBusiness.InsertDmNgheNghiep(dmNgheNghiepEntity);
                Validation.ClearControl(this);
                txtMaNgheNghiep.Focus();
            }
            else
            {
                dmNgheNghiepBusiness.EditDmNgheNghiep(dmNgheNghiepEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmNgheNghiepEvent != null)
                dmNgheNghiepEvent(sender, e);
        }

    }
}